import moment from 'moment';
import type { Ref } from 'vue';

export function longDateTimeToString(datetime: number): string {
  return moment(new Date(datetime)).format('yyyy-MM-DD HH:mm:ss');
}

export function longDateToString(datetime: number): string {
  return moment(new Date(datetime)).format('yyyy-MM-DD');
}

export function longMonthToString(datetime: number): string {
  return moment(new Date(datetime)).format('yyyy-MM');
}

export function setQuickQuery(dateRef: Ref<any>, dateType: string) {
  if (dateType === 'today') {
    const date = new Date();
    date.setHours(0, 0, 0, 0);
    dateRef.value.startTime = date.getTime();
    date.setHours(23, 59, 59, 0);
    dateRef.value.endTime = date.getTime();
  } else if (dateType === 'yesterday') {
    const date = new Date();
    date.setDate(date.getDate() - 1);
    date.setHours(0, 0, 0, 0);
    dateRef.value.startTime = date.getTime();
    date.setHours(23, 59, 59, 0);
    dateRef.value.endTime = date.getTime();
  } else if (dateType === 'thisWeek') {
    let date = moment().startOf('week').toDate();
    date.setHours(0, 0, 0, 0);
    dateRef.value.startTime = date.getTime();
    date = moment().endOf('week').toDate();
    date.setHours(23, 59, 59, 0);
    dateRef.value.endTime = date.getTime();
  } else if (dateType === 'lastWeek') {
    let date = moment().subtract(1, 'weeks').startOf('week').toDate();
    date.setHours(0, 0, 0, 0);
    dateRef.value.startTime = date.getTime();
    date = moment().subtract(1, 'weeks').endOf('week').toDate();
    date.setHours(23, 59, 59, 0);
    dateRef.value.endTime = date.getTime();
  } else if (dateType === 'thisMonth') {
    let date = moment().startOf('month').toDate();
    date.setHours(0, 0, 0, 0);
    dateRef.value.startTime = date.getTime();
    date = moment().endOf('month').toDate();
    date.setHours(23, 59, 59, 0);
    dateRef.value.endTime = date.getTime();
  } else if (dateType === 'lastMonth') {
    let date = moment().subtract(1, 'months').startOf('month').toDate();
    date.setHours(0, 0, 0, 0);
    dateRef.value.startTime = date.getTime();
    date = moment().subtract(1, 'months').endOf('month').toDate();
    date.setHours(23, 59, 59, 0);
    dateRef.value.endTime = date.getTime();
  }
}
